package com.ironman.mes.convention;

import com.ironman.mes.entity.Res;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.exception.StatusException;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Filter;
import org.noear.solon.core.handle.FilterChain;

@Slf4j
@Component
public class GlobalExceptionFilter implements Filter {
    @Override
    public void doFilter(Context ctx, FilterChain chain) throws Throwable {
        try {
            chain.doFilter(ctx);
        } catch (StatusException e) {
            ctx.render(Res.failure(String.valueOf(e.getCode()), e.getMessage()));
        } catch (Throwable e) {
            log.error(e.getMessage(), e);
            ctx.render(Res.failure("500", "服务端运行出错"));
        }
    }
} 